package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oauth.signpost.OAuth;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

import cn.edu.nuc.modle.WeiBoData;
import cn.edu.nuc.modle.WeiBoList;

import com.google.gson.Gson;

public class OtherUsersTimeLineActivity extends ListActivity {

	private SharedPreferences prefs = null;
	private long pageTime = 0;
	private boolean isUpdating = false;
	private ListView listView = null;
	private WeiBoItemAdapter adapter = null;
	private List<WeiBoData> weiBoDatas = null;
	private int pageflag = 0;
	private int lastid = 0;
	private int endpage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		weiBoDatas = getWeiBoDatasByPagetime(pageTime);

		adapter = new WeiBoItemAdapter(this, weiBoDatas);
		listView = getListView();
		listView.setAdapter(adapter);
		listView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem + visibleItemCount == totalItemCount
						&& !isUpdating && endpage == 0) {
					AsyncUpdateWeiBoDatasTask asyncUpdateWeiBoDatasTask = new AsyncUpdateWeiBoDatasTask();
					asyncUpdateWeiBoDatasTask.execute();
				}
			}
		});
		pageflag = 1;
		lastid = 1;

	}

	private List<WeiBoData> getWeiBoDatasByPagetime(long pageTime) {
		Map<String, String> keyValues = new HashMap<String, String>();
		keyValues.put("name", "Palytoxin");
		keyValues.put("format", "json");
		keyValues.put("pageflag", pageflag + "");
		keyValues.put("lastid", lastid + "");
		keyValues.put("type", "0");
		keyValues.put("contenttype", "0");
		WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY,
				Constants.CONSUMER_SECRET, prefs.getString(OAuth.OAUTH_TOKEN,
						""), prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""));
		String result = weiBoClient.doGet(
				Constants.WeiBoApi.OTHERUSERS_TIMELINE, keyValues);
		System.out.println("result------->-------------------------" + result);
		Gson gson = new Gson();
		WeiBoList weiBoList = gson.fromJson(result, WeiBoList.class);
		List<WeiBoData> weiBoDatas = weiBoList.getData().getInfo();
		endpage = weiBoList.getData().getHasnext();
		WeiBoData weiBoData = weiBoDatas.get(weiBoDatas.size() - 1);
		this.pageTime = weiBoData.getTimestamp();
		return weiBoDatas;
	}

	class AsyncUpdateWeiBoDatasTask extends
			AsyncTask<Void, Void, List<WeiBoData>> {

		protected List<WeiBoData> doInBackground(Void... params) {
			isUpdating = true;
			return getWeiBoDatasByPagetime(pageTime);
		}

		protected void onPostExecute(List<WeiBoData> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			weiBoDatas.addAll(result);
			adapter.notifyDataSetChanged();
			isUpdating = false;
			System.out.println("end update--------------");
		}

	}

}
